package com.study.crypto.basic.entity;

import com.study.crypto.basic.dto.ca.RequestSealCertDistinctNameDto;
import com.study.crypto.basic.dto.ca.RequestSealCertDto;
import com.study.crypto.basic.utils.CertUtils;
import com.study.crypto.basic.utils.EssPdfUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.springframework.beans.BeanUtils;

import javax.persistence.Column;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 表名：bo_certificate_apply
 * @author Songjin
 * @since 2021-01-10 14:11
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@SuperBuilder
@Table(name = "bo_certificate_apply")
public class CertificateApply extends GeneralEntity {
    
    /** 业务类型 */
    @Column(name = "task_code")
    private String taskCode;

    /** 协议版本号1.0 */
    @Column(name = "version")
    private String version;

    /** 客户端随机数与服务端随机数字符串拼接，中间无分隔符 */
    @Column(name = "token_info")
    private String tokenInfo;

    /** 用于区分请求列表的单个请求 */
    @Column(name = "request_id")
    private String requestId;

    /** 证书类型: 0 个人证书，1 单位证书，2 服务器证书，大于2预留扩展 */
    @Column(name = "cert_type")
    private String certType;

    /** 证书主体的国家项，2字节CN */
    @Column(name = "country_name")
    private String countryName;

    /** 证书主体的组织项，不大于 150 个字节 */
    @Column(name = "organization_name")
    private String organizationName;

    /** 证书主体的组织项，不大于200个字节 */
    @Column(name = "common_name")
    private String commonName;

    /** 证书主体DN */
    @Column(name = "dn")
    private String dn;

    /** 证书有效期起始时间，格式: yyyy-MM-dd HH:mm:ss */
    @Column(name = "not_before")
    private String notBefore;

    /** 证书有效期结束时间，格式: yyyy-MM-dd HH:mm:ss */
    @Column(name = "not_after")
    private String notAfter;

    /** SM2椭圆曲线公钥密码算法OID，1.2.156.10197.1.301 */
    @Column(name = "algorithm")
    private String algorithm;

    /** base64编码的签名公钥信息 */
    @Column(name = "subject_public_key_info")
    private String subjectPublicKeyInfo;
    
    /** 密钥外键 */
    @Column(name = "keystore_id")
    private String keystoreId;
    
    /** CA系统返回的任务编号 */
    @Column(name = "task_id")
    private String taskId;
    
    public static List<CertificateApply> getInstances(RequestSealCertDto request) {
        List<CertificateApply> list = new ArrayList<>();
        request.getData().forEach(data -> {
            CertificateApply apply = new CertificateApply();
            apply.setId(EssPdfUtil.genRandomUuid());
            BeanUtils.copyProperties(data, apply);
            BeanUtils.copyProperties(request, apply);
            list.add(apply);
        });
        return list;
    }

    public static List<CertificateApply> getInstances(RequestSealCertDistinctNameDto request) {
        List<CertificateApply> list = new ArrayList<>();
        request.getData().forEach(data -> {
            CertificateApply apply = new CertificateApply();
            apply.setId(EssPdfUtil.genRandomUuid());
            Map<ASN1ObjectIdentifier, String> dnMap = CertUtils.splitDn(data.getDn());
            BeanUtils.copyProperties(data, apply);
            BeanUtils.copyProperties(request, apply);
            apply.setCountryName(dnMap.get(BCStyle.C));
            apply.setCommonName(dnMap.get(BCStyle.CN));
            apply.setOrganizationName(dnMap.get(BCStyle.O));
            list.add(apply);
        });
        return list;
    }

}